package com.zzz.common.core.domain;

import com.zzz.common.core.enums.ResultCode;
import lombok.Data;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.conmmon.core.domain
 * @Author: zhai
 * @CreateTime: 2025-09-21  17:37
 * @Description: TODO 统一返回结果
 * @Version: 1.0
 */
@Data
public class R<T> {
    private Integer code; // 状态码
    private String msg; // 提示信息
    private T data; //  数据

    /**
     * 成功返回结果
     */
    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setData(data);
        return r;
    }

    public static <T> R<T> success(String msg, T data) {
        R<T> r = new R<>();
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> success(ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        return r;
    }

    public static R<Void> success() {
        R<Void> r = new R<>();
        r.setCode(ResultCode.SUCCESS.getCode()); // 修复：添加状态码设置
        r.setMsg(ResultCode.SUCCESS.getMsg());   // 修复：添加消息设置
        return r;
    }

    /**
     * 错误返回结果
     */
    public static <T> R<T> fail(T data) {
        R<T> r = new R<>();
        r.setCode(ResultCode.FAILED.getCode());
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(String msg, T data) {
        R<T> r = new R<>();
        r.setCode(ResultCode.FAILED.getCode());
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(Integer code , String msg, T data) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        return r;
    }

    public static <T> R<T> fail(String msg) {
        R<T> r = new R<>();
        r.setMsg(msg);
        return r;
    }

    public static R<Void> fail(int code, String message) {
        R<Void> r = new R<>();
        r.setCode(code);
        r.setMsg(message);
        return r;
    }

    public static R<Void> fail() {
        R<Void> r = new R<>();
        return r;
    }
}
